//
//  ProductDetailView.m
//  SP2P
//
//  Created by 吴建东 on 16/6/8.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "ProductDetailView.h"
#import "ProdectDetailCell.h"

@interface ProductDetailView ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSMutableArray *_titleDataArray;
    NSMutableArray *_detailDataArray;
    NSMutableArray *_imageDataArray;
    
    UILabel *_rateLabel;
    
}
@end

@implementation ProductDetailView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initData];
        [self initView];
    }
    
    return self;
}

- (void)transButtonClick {
    if (self.deleagate && [self.deleagate respondsToSelector:@selector(transButtonClick)]) {
        [self.deleagate transButtonClick];
    }
}

- (void)setDataDict:(NSDictionary *)dataDict {
    _dataDict = dataDict;
    
    if ([_dataDict objectForKey:@"apr"]) {
        NSString *rateStr = [NSString stringWithFormat:@"%.1f%%",[_dataDict[@"apr"] floatValue]];
        _rateLabel.attributedText = [MyTools getNSMutableAttributedStringWithText:rateStr start:rateStr.length-1 end:rateStr.length textColor:[UIColor whiteColor] textFont:KFontSize(15)];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_titleDataArray.count) {
        static NSString *cellID = @"cellID";
        ProdectDetailCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"ProdectDetailCell" owner:self options:nil][0];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.titleLabel.text = _titleDataArray[indexPath.row];
        cell.detailLabel.text = _detailDataArray[indexPath.row];
        cell.typeImageView.image = [UIImage imageNamed:_imageDataArray[indexPath.row]];
        
        cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, KAdaptNum(44));
        return cell;
    }else {
        EmptyTableViewCell *cell = [[EmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"emptyCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = KbackgroundColor;
        
        cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, _tableView.frame.size.height);
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1.0f;
}

- (void)initData {
    _titleDataArray = [[NSMutableArray alloc] initWithArray:@[@"产品名称",@"计息时间",@"锁定期限",@"投资起限"]];
    _detailDataArray = [[NSMutableArray alloc] initWithArray:@[@"随取宝",@"即时计息",@"满24小时后随存随取",@"单笔100元起投"]];
    _imageDataArray = [[NSMutableArray alloc] initWithArray:@[@"productdetail_name",@"productdetail_profittime",@"productdetail_locktime",@"productdetail_investtime"]];
}

- (void)initView {
    _tableView = [UITableView tableViewWithFrmae:RECTMAKE(0, 0, MSWIDTH, self.frame.size.height) backgroundColor:KbackgroundColor delegate:self dataSource:self tableViewStyle:UITableViewStylePlain separatorStyle:UITableViewCellSeparatorStyleNone superview:self];
    _tableView.bounces = NO;
    
    _tableView.tableHeaderView = [self loadHeaderView];
    _tableView.tableFooterView = [self loadFooterView];
}

- (UIView *)loadHeaderView {
    UIView *headerView = [UIView viewWithFrame:RECTMAKE(0, 0, MSWIDTH, KAdaptNum(170)) backgroundColor:[UIColor whiteColor] superview:nil];
    
    UIImageView *backImageView = [UIImageView imageViewWithFrame:RECTMAKE((MSWIDTH - KAdaptNum(95))/2, KAdaptNum(30), KAdaptNum(95), KAdaptNum(95)) imageFile:@"productdetail_rate_back" superview:headerView];
    
    UILabel *rateValue = [UILabel labelWithFrame:RECTMAKE(5, KAdaptNum(20), GetWidth(backImageView) - 10, KAdaptNum(15)) text:@"7日年化收益" textColor:[UIColor whiteColor] textFont:10 fitWidth:YES superview:backImageView];
    rateValue.textAlignment = NSTextAlignmentCenter;
    
    _rateLabel = [UILabel labelWithFrame:RECTMAKE(5, GetMaxY(rateValue) + KAdaptNum(5) , GetWidth(backImageView) - 10, KAdaptNum(30)) text:@"0.0" textColor:[UIColor whiteColor] textFont:30 fitWidth:YES superview:backImageView];
    _rateLabel.textAlignment = NSTextAlignmentCenter;
    
//    UILabel *countValue = [UILabel labelWithFrame:RECTMAKE(5, KAdaptNum(135), GetWidth(headerView) - 10, KAdaptNum(20)) text:@"算算1万元30天赚多少" textColor:KColor textFont:12 fitWidth:YES superview:headerView];
//    countValue.textAlignment = NSTextAlignmentCenter;
    
    return headerView;
}


- (UIView *)loadFooterView {
    UIView *footerView = [UIView viewWithFrame:RECTMAKE(0, 0, MSWIDTH, KAdaptNum(104)) backgroundColor:KbackgroundColor superview:nil];
 
    ClickButton *transButton = [[ClickButton alloc] initWithFrame:RECTMAKE(15, KAdaptNum(40), MSWIDTH - 30, KAdaptNum(44))];
    [transButton setTitle:@"立即转入" forState:UIControlStateNormal];
    [transButton addTarget:self action:@selector(transButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:transButton];
    
    return footerView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
